<template>
<section>
  <el-row>
    <el-col :span="6">
      <el-input placeholder="请输入内容" v-model="searchKey" prefix-icon="el-icon-fa-search">
        <div slot="append">
          <el-button type="primary" icon="el-icon-fa-search" @click=" pager.pager.pageNumber = 1 ;doSearch()"></el-button>
        </div>
      </el-input>
    </el-col>
    <el-col :span="6" :offset="12">
      <el-button type="primary" icon="el-icon-fa-plus" @click="addstorage" size="small">添加仓储信息</el-button>
    </el-col>
  </el-row>
  <el-table :data="pager.dataList" border stripe style="width: 100%">
    <el-table-column prop="id" label="ID" header-align="center" align="center" width="55">
    </el-table-column>
    <el-table-column prop="name" label="名称" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="temperature" label="温度" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="humidity" label="湿度" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column label="操作" show-overflow-tooltip header-align="center" align="center">
      <template slot-scope="scope">
        <el-button-group>
          <el-button title="编辑仓储信息" size="mini" type="primary" icon="el-icon-fa-edit" @click="handleEdit(scope.$index,scope.row)"></el-button>
          <el-button title="删除" size="mini" type="primary" icon="el-icon-fa-trash" @click="handleDelete(scope.$index,scope.row)"></el-button>
        </el-button-group>
      </template>
    </el-table-column>
  </el-table>
  <!--分页条 -->
  <el-row>
    <el-col :span="6" :offset="18">
      <el-pagination background small style="float:right" layout="prev, pager, next" :total="pager.pager.recordCount" :page-size="pager.pager.pageSize" :current-page.sync="pager.pager.pageNumber" v-show="pager.pager.pageCount != 0" @current-change="changePage">
      </el-pagination>
    </el-col>
  </el-row>
  <!-- 弹框区域-->
  <el-dialog :title="storage.id == 0 ? '添加仓储信息' : '编辑仓储信息' " :visible.sync="addEditShow" width="50%">
    <el-form :model="storage" :rules="$rules.storage" ref="storageForm">
      <el-form-item label="仓库名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="storage.name" auto-complete="off" readonly suffix-icon="el-icon-fa-vcard" placeholder="请填写仓库名称"></el-input>
      </el-form-item>
      <el-form-item label="温度" :label-width="formLabelWidth" prop="temperature">
        <el-input v-model="storage.temperature" auto-complete="off" placeholder="请填写温度">
              <template slot="append">℃</template>
        </el-input>
      </el-form-item>
      <el-form-item label="湿度" :label-width="formLabelWidth" prop="humidity">
        <el-input v-model="storage.humidity" auto-complete="off"  placeholder="请填写湿度">
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
      <el-form-item label="仓库图片" :label-width="formLabelWidth" prop="imgKey">
        <el-upload class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'imgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="storage.imgKey" :src="storage.imgKey" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-upload v-show="false" class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'hcbciImgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="addEditShow = false ; storage = {id:0, name:'马戏咖啡'}">取 消</el-button>
      <el-button type="primary" @click="saveOrUpdatestorage('storageForm')">确 定</el-button>
    </div>
  </el-dialog>
</section>
</template>

<style>
</style>

<script>
import config from "../../common/config";
export default {
  data() {
    return {
      imgupload: config.http.baseUrl + "image/kind/upload",
      searchKey: "",
      pager: {
        pager: {
          pageCount: 1,
          pageNumber: 1,
          pageSize: 15,
          recordCount: 1
        },
        dataList: []
      },
      addEditShow: false,
      imageUrl: "",
      formLabelWidth: "100px",
      storage: {
        id: 0,
        name: "马戏咖啡"
      },
      areas: []
    };
  },
  methods: {
    loadData() {
      this.$api.Storage.list(this.pager.pager.pageNumber, result => {
        this.pager = result.pager;
      });
    },
    addstorage() {
      this.addEditShow = true;
      this.storage = {
        id: 0,
        name: "马戏咖啡"
      };
    },
    handleEdit(index, row) {
      this.storage = row;
      this.addEditShow = true;
    },
    handleDelete(index, row) {
      this.$confirm("确认删除该仓库?", "删除确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$api.Storage.delete(row.id, result => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          window.setTimeout(() => {
            if (this.searchKey) {
              this.doSearch();
            } else {
              this.loadData();
            }
          }, 2000);
        });
      });
    },
    changePage() {
      if (this.searchKey) {
        this.doSearch();
      } else {
        this.loadData();
      }
    },
    doSearch() {
      this.$api.Storage.search(
        this.pager.pager.pageNumber,
        this.searchKey,
        result => {
          this.pager = result.pager;
        }
      );
    },
    saveOrUpdatestorage(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var callback = result => {
            this.changePage();
            this.addEditShow = false;
          };
          this.storage.id
            ? this.$api.Storage.update(this.storage, callback)
            : this.$api.Storage.save(this.storage, callback);
        } else {
          return false;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.storage[res.key] = config.http.baseUrl + res.url;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isIMG = file.type.substr(0, "image/".length) === "image/";
      console.log(isIMG);
      const isLt5M = file.size / 1024 / 1024 < 5;

      if (!isIMG) {
        this.$message.error("只能上传图片格式!");
      }
      if (!isLt5M) {
        this.$message.error("上传图片大小不能超过 5 MB!");
      }
      return isIMG && isLt5M;
    }
  },
  created() {
    this.loadData();
  }
};
</script>
